import { CART_LIST, CHECK_ALL, UN_CHECK_ALL, CHECK_ITEM } from './mutations-types.js'
// import { Toast } from 'mint-ui';
import { Toast,Dialog  } from 'vant';

export default {
    state: {
        list: [], //购物车数据
        selectList: [] //选中数据或者数据
    },
    getters: {
        isCheckedAll( state ) {
            return state.list.length == state.selectList.length;
        },
        total( state ) {
            let total = {
                num: 0,
                price: 0
            }
            state.list.forEach(v=>{
                if(v.checked) {
                    total.num += parseInt(v.goods_num);
                    total.price += v.goods_price * v.goods_num
                }
            })
            return total
        }
    },
    mutations: {
        [CART_LIST](state, car) {
            state.list = car
            car.forEach(v=>{
                state.selectList.push(v.id)
            })
        },
        // 全选
        [CHECK_ALL]( state ) {
            // state.selectList    存id的
            state.selectList = state.list.map(v => {
                v.checked = true
                return v.id
            });
            
        },
        // 全不选
        [UN_CHECK_ALL]( state ) {
            state.list.forEach(v => {
                v.checked = false
            })
            state.selectList = []
        },
        // 单选
        [CHECK_ITEM] (state, index) {
            let id = state.list[index].id
            let i = state.selectList.indexOf(id)
            // 能在selecList里找到对应的id， 就删除
            if( i > -1 ) {
                return state.selectList.splice(i, 1)
            } else {
                // 如果之前没有选中,就给selecList添加一个
                state.selectList.push(id)
            }
            // console.log(state.list[index], state.selectList)
        }
    },
    actions: {
        checkAllFn({commit, getters}) {
            getters.isCheckedAll ? commit('UN_CHECK_ALL') : commit('CHECK_ALL')
        },
        // 删除操作
        delGoods({state}, id) {
            // 如果没有被选中，则提示信息
            if(state.selectList.length == 0) {
                Toast('请选择商品');
            }

            Dialog.confirm({
                title: '标题',
                message: '弹窗内容',
              })
                .then(() => {
                  // on confirm
                })
                .catch(() => {
                  // on cancel
                });
            console.log(id)
        }
    }
}